package com.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.constrant.ProductConstant;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;
import com.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.gulimall.product.dao.AttrDao;
import com.gulimall.product.dao.AttrGroupDao;
import com.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.entity.AttrGroupEntity;
import com.gulimall.product.service.AttrGroupService;
import com.gulimall.product.service.CategoryService;
import com.gulimall.product.vo.AttrRelationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
@Slf4j
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationDao relationDao;
    @Autowired
    private AttrDao attrDao;

    @Override

    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>());

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long category) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        String key = (String) params.get("key");
        if (StringUtils.hasText(key)) {
            queryWrapper.and(obj -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (category == 0) {
            return new PageUtils(this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper));
        } else {
            queryWrapper.eq("catelog_Id", category);
            return new PageUtils(this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper));
        }
    }

    @Override
    public AttrGroupEntity getGroupPaths(Long attrGroupId) {
        long[] longs = categoryService.queryCatelogPath(attrGroupId);
        AttrGroupEntity attrGroupEntity = getById(attrGroupId);
        attrGroupEntity.setCatelogPath(longs);
        return attrGroupEntity;
    }

    @Override
    public List<AttrEntity> getAttrRelation(Long catelogId) {
        List<AttrAttrgroupRelationEntity> relationEntities =
                relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", catelogId));

        List<Long> longList =
                relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

        if (longList.size() > 0) {
            return attrDao.selectBatchIds(longList);
        }
        return null;
    }

    @Override
    @Transactional
    public void matchDeleteRelation(List<AttrRelationVo> relationVo) {
        relationDao.batchRemove(relationVo);
    }

    @Override
    public PageUtils getNoattrRelation(Map<String, Object> params, Long attrGroupId) {
        //找到当前分组所属的分类
        AttrGroupEntity groupEntity = this.getById(attrGroupId);
        Long catelogId = groupEntity.getCatelogId();

        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("catelog_id", catelogId).eq(
                "attr_type", ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());

        String key = (String) params.get("key");

        if (StringUtils.hasText(key)) {
            queryWrapper.and(wrapper -> {
                wrapper.eq("attr_id", key).or().like("attr_name", key);
            });
        }

        //找到此分类下的所有的基本属性
        List<AttrEntity> attrEntities = attrDao.selectList(queryWrapper);

        List<Long> longList = attrEntities.stream().map(AttrEntity::getAttrId).collect(Collectors.toList());
        //找到这些属性关联的分组
        List<AttrAttrgroupRelationEntity> relationEntities =
                relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_id", longList));

        List<Long> relationIds =
                relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

        if (relationIds.size() > 0) {
            queryWrapper.and((obj) -> {
                obj.notIn("attr_id", relationIds);
            });
        }
        IPage<AttrEntity> iPage = attrDao.selectPage(new Query<AttrEntity>().getPage(params), queryWrapper);
        return new PageUtils(iPage);
    }

    @Override
    public List<AttrGroupEntity> queryAttrGroupByCatelogId(Long catelogId) {
        return list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
    }
}
